
package controllers;

import java.util.LinkedList;
import java.util.List;

import org.bouncycastle.openssl.PEMReader;

import models.Entities;
import models.Organization;
import models.Permission;
import models.SearchResultTag;
import models.Tag;
import models.Topic;
import models.UnRegisteredEmail;
import models.User;
import play.mvc.Controller;

public class Application extends Controller {

	public static void index() {
		render();
	}

	/**
	 * The below methods starts by taking the page parameter and then checks
	 * which text boxes are null and which are not and accordingly it executes
	 * the related method from the class Tag rendering the list of results of
	 * tags
	 * 
	 * @param page
	 *            it defines the number of page in which the results are in.
	 * @author Ismail Omar
	 */
	public static void searchForTag(Integer page) {
		String search = params.get("search5");
		String searchOne = params.get("search6");
		String searchTwo = params.get("search7");
		String searchThree = params.get("search8");
		if (search != null && searchOne == null && searchTwo == null
				&& searchThree == null) {
			if (page == null)
				page = 1;
			SearchResultTag results = Tag.searchForTag(search, page);
			List<Tag> listTag = results.tags;
			render(results, listTag, page, search);
		}

		if (search != null && searchOne != null && searchTwo == null
				&& searchThree == null) {
			if (page == null)
				page = 1;
			SearchResultTag results = Tag.searchForTagOne(search, searchOne,
					page);
			List<Tag> listTag = results.tags;
			render(results, listTag, page, searchOne);
		}

		if (search != null && searchOne != null && searchTwo != null
				&& searchThree == null) {
			if (page == null)
				page = 1;
			SearchResultTag results = Tag.searchForTagTwo(search, searchOne,
					searchTwo, page);
			List<Tag> listTag = results.tags;
			render(results, listTag, page, searchTwo);
		}

		if (search != null && searchOne != null && searchTwo != null
				&& searchThree != null) {
			if (page == null)
				page = 1;
			SearchResultTag results = Tag.searchForTagThree(search, searchOne,
					searchTwo, searchThree, page);
			List<Tag> listTag = results.tags;
			render(results, listTag, page, searchThree);
		}
	}

	/**
	 * The below method takes an id as a parameter and find the corresponding
	 * tag and show it in a new Page upon clicking on it
	 * 
	 * @param id
	 *            the Tag id
	 * @author ismail omar
	 */
	public static void showTag(Long id) {
		Tag tag = Tag.findById(id);
		render(tag);
	}

	/**
	 * The below method is the template for the textbox and the button for the
	 * advanced search for Tag
	 * 
	 * @author ismail omar
	 */
	public static void advancedSearchForTag() {
		render();
	}

	public static void login() {

		render();
	}

	/**
	 * the below method take the organization's id and sends the user from
	 * the showOrg view to the OrgLead view
	 * 
	 * @param id
	 * 		Organization's id
	 * 
	 * @author Karim Ezzat
	 */
	public static void OrgLead(Long id) {
		Organization organization = Organization.findById(id);
		render(organization);
	}

	/**
	 * The below method takes an id as a parameter and gets the corresponding
	 * organization and then it gets the corresponding entities in this
	 * organization
	 * 
	 * @param id
	 *            Organization id
	 * @author karim ezzat
	 */
	public static void getOrgEntities(Long id) {
		Organization organization = Organization.findById(id);
		List<Entities> entityList = organization.allEntities;
		render(entityList);
	}

	/**
	 * The below method takes a user email, entity name and a permissions
	 * as parameters, then gets the corresponding user_id and entity_id
	 * and then gets the corresponding row in the permission table with 
	 * the user_id and entity_id. Last, it changes the permission that was
	 * taken in the param to true.
	 * 
	 * @param email
	 *            Organizer's email
	 * @param entityName
	 * 			  Entity's name
	 * @param permission
	 * 			  Permission
	 * 
	 * @author karim ezzat
	 */
	public static void givePermission(String email, String entityName,
			String permission) {
		User user = User.find("email=?", email).first();
		Entities entity = Entities.find("entityName=?", entityName).first();
		long userId = user.id;
		long entityId = entity.id;
		if ((Permission.find(
				"user_id = ? AND entity_id = ? AND aUsertype='organizer'",
				userId, entityId).fetch()) != null) {
			Permission temp = Permission.find(
					"user_id = ? AND entity_id = ? AND aUsertype='organizer' ",
					userId, entityId).first();
			if (permission != null) {

				if (permission.equals("acceptOrEditRequestForTopic")) {
					temp.acceptOrEditRequestForTopic = true;
				}

				if (permission.equals("addIdeasToEntity")) {
					temp.addIdeasToEntity = true;
				}
				if (permission.equals("addIdeasToOrganization")) {
					temp.addIdeasToOrganization = true;
				}
				if (permission.equals("addIdeasToTopic")) {
					temp.addIdeasToTopic = true;
				}
				if (permission.equals("addOrDeleteComment")) {
					temp.addOrDeleteComment = true;
				}
				if (permission.equals("addOrganizer")) {
					temp.addOrganizer = true;
				} 
				if (permission.equals("addOrganizationLead")) {
					temp.addOrganizationLead = true;
				}
				if (permission.equals("addOrganizationPreferences")) {
					temp.addOrganizationPreferences = true;
				}
				if (permission.equals("addTagOrUntagIdea")) {
					temp.addTagOrUntagIdea = true;
				}
				if (permission.equals("approveOrRejectRelationRequest")) {
					temp.approveOrRejectRelationRequest = true;
				}
				if (permission.equals("blockUse")) {
					temp.blockUse = true;
				}
				if (permission.equals("blockView")) {
					temp.blockView = true;
				}
				if (permission.equals("confirmDuplicate")) {
					temp.confirmDuplicate = true;
				}
				if (permission.equals("createInfo")) {
					temp.createInfo = true;
				}
				if (permission.equals("createOrEditEntities")) {
					temp.createOrEditEntities = true;
				}
				if (permission.equals("createOrEditOrganizationProfile")) {
					temp.createOrEditOrganizationProfile = true;
				}
				if (permission.equals("createOrganization")) {
					temp.createOrganization = true;
				}
				if (permission.equals("createRelation")) {
					temp.createRelation = true;
				}
				if (permission.equals("createRelationName")) {
					temp.createRelationName = true;
				}
				if (permission.equals("createTag")) {
					temp.createTag = true;
				}
				if (permission.equals("defineOrganizationType")) {
					temp.defineOrganizationType = true;
				}
				if (permission.equals("deleteEntities")) {
					temp.deleteEntities = true;
				}
				if (permission.equals("deleteOrEditTagNoOneIsUsing")) {
					temp.deleteOrEditTagNoOneIsUsing = true;
				}
				if (permission.equals("deleteORhideidea")) {
					temp.deleteOrHideIdea = true;
				}
				if (permission.equals("deleteOwnIdeas")) {
					temp.deleteOwnIdeas = true;
				}
				if (permission.equals("deleteProfile")) {
					temp.deleteProfile = true;
				}
				if (permission.equals("deleteTopicHasNoIdeas")) {
					temp.deleteTopicHasNoIdeas = true;
				}
				if (permission.equals("editInfo")) {
					temp.editInfo = true;
				}
				if (permission.equals("execute")) {
					temp.execute = true;
				}
				if (permission.equals("generateInvitation")) {
					temp.generateInvitation = true;
				}
				if (permission.equals("hideTopic")) {
					temp.hideTopic = true;
				}
				if (permission.equals("inviteToBeIdeaDeveloper")) {
					temp.inviteToBeIdeaDeveloper = true;
				}
				if (permission.equals("inviteToBeOrganizer")) {
					temp.inviteToBeOrganizer = true;
				}
				if (permission.equals("lockOrUnlock")) {
					temp.lockOrUnlock = true;
				}
				if (permission.equals("mergeIdeas")) {
					temp.mergeIdeas = true;
				}
				if (permission.equals("postTopic")) {
					temp.postTopic = true;
				}
				if (permission.equals("prioritizeIdeas")) {
					temp.prioritizeIdeas = true;
				}
				if (permission.equals("promoteIdea")) {
					temp.promoteIdea = true;
				}
				if (permission.equals("rateRequestOrIdea")) {
					temp.rateRequestOrIdea = true;
				}
				if (permission.equals("renameOrUnassignRelations")) {
					temp.renameOrUnassignRelations = true;
				}
				if (permission.equals("report")) {
					temp.report = true;
				}
				if (permission.equals("requestRelationName")) {
					temp.requestRelationName = true;
				}
				if (permission.equals("shareRequestOrIdea")) {
					temp.shareRequestOrIdea = true;
				}
				if (permission.equals("specifyRelations")) {
					temp.specifyRelations = true;
				}
				temp.save();

			}
		}
		render();
	}

	/**
	 * The below method takes a user email, entity name and a permissions
	 * as parameters, then gets the corresponding user_id and entity_id
	 * and then gets the corresponding row in the permission table with 
	 * the user_id and entity_id. Last, it changes the permission that was
	 * taken in the param to false.
	 * 
	 * @param email
	 *            Organizer's email
	 * @param entityName
	 * 			  Entity's name
	 * @param permission
	 * 			  Permission
	 * 
	 * @author karim ezzat
	 */
	public static void restrictPermission(String email, String entityName,
			String permission) {
		User user = User.find("email=?", email).first();
		Entities entity = Entities.find("entityName=?", entityName).first();
		long userId = user.id;
		long entityId = entity.id;
		if ((Permission.find(
				"user_id = ? AND entity_id = ? AND aUsertype='organizer'",
				userId, entityId).fetch()) != null) {
			Permission temp = Permission.find(
					"user_id = ? AND entity_id = ? AND aUsertype='organizer'",
					userId, entityId).first();
			if (permission != null) {

				if (permission.equals("acceptOrEditRequestForTopic")) {
					temp.acceptOrEditRequestForTopic = false;
				}

				if (permission.equals("addIdeasToEntity")) {
					temp.addIdeasToEntity = false;
				}
				if (permission.equals("addIdeasToOrganization")) {
					temp.addIdeasToOrganization = false;
				}
				if (permission.equals("addIdeasToTopic")) {
					temp.addIdeasToTopic = false;
				}
				if (permission.equals("addOrDeleteComment")) {
					temp.addOrDeleteComment = false;
				}
				if (permission.equals("addOrganizer")) {
					temp.addOrganizer = false;
				} 
				if (permission.equals("addOrganizationLead")) {
					temp.addOrganizationLead = false;
				}
				if (permission.equals("addOrganizationPreferences")) {
					temp.addOrganizationPreferences = false;
				}
				if (permission.equals("addTagOrUntagIdea")) {
					temp.addTagOrUntagIdea = false;
				}
				if (permission.equals("approveOrRejectRelationRequest")) {
					temp.approveOrRejectRelationRequest = false;
				}
				if (permission.equals("blockUse")) {
					temp.blockUse = false;
				}
				if (permission.equals("blockView")) {
					temp.blockView = false;
				}
				if (permission.equals("confirmDuplicate")) {
					temp.confirmDuplicate = false;
				}
				if (permission.equals("createInfo")) {
					temp.createInfo = false;
				}
				if (permission.equals("createOrEditEntities")) {
					temp.createOrEditEntities = false;
				}
				if (permission.equals("createOrEditOrgProfile")) {
					temp.createOrEditOrganizationProfile = false;
				}
				if (permission.equals("createOrg")) {
					temp.createOrganization = false;
				}
				if (permission.equals("createRelation")) {
					temp.createRelation = false;
				}
				if (permission.equals("createRelationName")) {
					temp.createRelationName = false;
				}
				if (permission.equals("createTag")) {
					temp.createTag = false;
				}
				if (permission.equals("defineOrganizationType")) {
					temp.defineOrganizationType = false;
				}
				if (permission.equals("deleteEntities")) {
					temp.deleteEntities = false;
				}
				if (permission.equals("deleteOrEditTagNoOneIsUsing")) {
					temp.deleteOrEditTagNoOneIsUsing = false;
				}
				if (permission.equals("deleteORhideidea")) {
					temp.deleteOrHideIdea = false;
				}
				if (permission.equals("deleteOwnIdeas")) {
					temp.deleteOwnIdeas = false;
				}
				if (permission.equals("deleteProfile")) {
					temp.deleteProfile = false;
				}
				if (permission.equals("deleteTopicHasNoIdeas")) {
					temp.deleteTopicHasNoIdeas = false;
				}
				if (permission.equals("editInfo")) {
					temp.editInfo = false;
				}
				if (permission.equals("execute")) {
					temp.execute = false;
				}
				if (permission.equals("generateInvitation")) {
					temp.generateInvitation = false;
				}
				if (permission.equals("hideTopic")) {
					temp.hideTopic = false;
				}
				if (permission.equals("inviteToBeIdeaDeveloper")) {
					temp.inviteToBeIdeaDeveloper = false;
				}
				if (permission.equals("inviteToBeOrganizer")) {
					temp.inviteToBeOrganizer = false;
				}
				if (permission.equals("lockOrUnlock")) {
					temp.lockOrUnlock = false;
				}
				if (permission.equals("mergeIdeas")) {
					temp.mergeIdeas = false;
				}
				if (permission.equals("postTopic")) {
					temp.postTopic = false;
				}
				if (permission.equals("prioritizeIdeas")) {
					temp.prioritizeIdeas = false;
				}
				if (permission.equals("promoteIdea")) {
					temp.promoteIdea = false;
				}
				if (permission.equals("rateRequestOrIdea")) {
					temp.rateRequestOrIdea = false;
				}
				if (permission.equals("renameOrUnassignRelations")) {
					temp.renameOrUnassignRelations = false;
				}
				if (permission.equals("report")) {
					temp.report = false;
				}
				if (permission.equals("requestRelationName")) {
					temp.requestRelationName = false;
				}
				if (permission.equals("shareRequestOrIdea")) {
					temp.shareRequestOrIdea = false;
				}
				if (permission.equals("specifyRelations")) {
					temp.specifyRelations = false;
				}
				temp.save();

			}
		}
		render();
	}
	/**
	 * The below method gets the Entity by the id given as a parameter and then
	 * gets its organizers and renders then in a list
	 * 
	 * @param id
	 *            Entity id
	 * @author karim ezzat
	 */

	public static void showEntity(Long id) {
		Entities entity = Entities.findById(id);
		List<User> organizersList = entity.entityOrganizers;
		render(entity, organizersList);
	}

	/**
	 * The below method renders the page show user in which the permissions are
	 * given or restricted and also the entity and the user are rendered too
	 * after finding them by their ids which are given in the parameters of this
	 * method
	 * 
	 * @param entityId
	 *            Entity id
	 * @param userId
	 *            Organizer id
	 * @author karim ezzat            
	 */
	public static void showUser(Long entityId, Long userId) {
		Entities entity = Entities.findById(entityId);
		User user = User.findById(userId);
		render(entity, user);
	}
}